﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Data;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using System.Text;
using Microsoft.SharePoint.Utilities;

namespace MII_FIF.Layouts.MII_FIF.FormApproval
{
    public partial class ApprovalDataCabang : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //((Label)Header.FindControl("lblHeader")).Text = "Form Approval Request Data Cabang";
            txtALamat.Attributes.Add("readonly", "true");
            btnApprove.Click += btnApprove_Click;
            btnReject.Click += btnReject_Click;
            btnCancel.Click += btnCancel_Click;
            //if (!Generic.isApprover())
            //{
            //    btnCancel_Click(sender, e);
            //}
            if (!IsPostBack)
            {
                load();
            }
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/FormApproval/Task.aspx"));
        }

        void btnReject_Click(object sender, EventArgs e)
        {
            lblError.Text = string.Empty;
            if (validasi())
            {
                if (txtComment.Text.Trim() == string.Empty)
                {
                    lblError.Text += "Please fill the comment.";
                }
                else
                {
                    InventoryManagement.Approval apps = new InventoryManagement.Approval();
                    InventoryManagement.Data_Cabang cabang = new InventoryManagement.Data_Cabang();
                    apps.ID = Guid.Parse(ViewState["ID"].ToString());
                    apps.Select();
                    cabang.ID = apps.IDChild;
                    cabang.Select();
                    if (apps.Approver != null)
                    {
                        lblError.Text += "This Request already Approved/Rejected, please refresh your browser. <br />";
                    }
                    else
                    {
                        cabang.wfStatus = word.Rejected;
                        apps.ApprovalDate = DateTime.Now;
                        apps.Approver = Generic.GetUserLoginReformat();
                        apps.Comment = txtComment.Text;
                        apps.Action = word.Reject;
                        apps.Update();
                        cabang.Update();
                        //#region sendemail
                        //#region headerEmail
                        //StringDictionary headers = new StringDictionary();
                        //headers.Add("to", Generic.getEmailUser(apps.Requester));
                        //headers.Add("cc", "");
                        //headers.Add("bcc", "");
                        //headers.Add("from", "");
                        //headers.Add("subject", "Request Rejected");
                        //headers.Add("content-type", "text/html");
                        //#endregion
                        #region bodyEmail
                        StringBuilder sb = new StringBuilder();
                        sb.Append("Request Data Cabang Rejected By " + SPContext.Current.Web.CurrentUser.Name);
                        Generic.sendEmailSMTP("System@fif.co.id", Generic.getEmailUser(apps.Requester), "Request Rejected", sb.ToString());
                        #endregion
                        //SPUtility.SendEmail(SPContext.Current.Web, headers, sb.ToString());
                        //#endregion
                        Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/FormApproval/Task.aspx"));
                    }

                }
            }
        }

        void btnApprove_Click(object sender, EventArgs e)
        {
            lblError.Text = string.Empty;
            if (validasi())
            {
                if (txtComment.Text.Trim() == string.Empty)
                {
                    lblError.Text += "Please fill the comment.";
                }
                else
                {
                    InventoryManagement.Approval apps = new InventoryManagement.Approval();
                    InventoryManagement.Data_Cabang cabang = new InventoryManagement.Data_Cabang();
                    apps.ID = Guid.Parse(ViewState["ID"].ToString());
                    apps.Select();
                    cabang.ID = apps.IDChild;
                    cabang.Select();
                    if (apps.Approver != null)
                    {
                        lblError.Text += "This Request already Approved/Rejected, please refresh your browser. <br />";
                    }
                    else
                    {
                        cabang.wfStatus = word.Approved;
                        apps.ApprovalDate = DateTime.Now;
                        apps.Approver = Generic.GetUserLoginReformat();
                        apps.Comment = txtComment.Text;
                        apps.Action = word.Approve;
                        apps.Update();
                        cabang.Update();

                        Response.Redirect(Generic.GenLink("/_Layouts/MII_FIF/FormApproval/Task.aspx"));
                    }
                }
            }
        }

        void load()
        {
            if (Request.QueryString["ID"] != null)
            {
                ViewState["ID"] = Request.QueryString["ID"].ToString();
                #region declare
                InventoryManagement.Approval apps = new InventoryManagement.Approval();
                apps.ID = Guid.Parse(ViewState["ID"].ToString());
                apps.Select();

                InventoryManagement.Data_Cabang Cabang = new InventoryManagement.Data_Cabang();
                Cabang.ID = apps.IDChild;
                Cabang.Select();

                if (Cabang.wfStatus.ToLower() == word.Approve.ToLower() || Cabang.wfStatus.ToLower() == word.Reject || Cabang.wfStatus.ToLower() == word.Rejected.ToLower() || Cabang.wfStatus.ToLower() == word.Approved)
                {
                    btnApprove.Visible = false;
                    btnReject.Visible = false;
                }


                InventoryManagement.Provider Provider = new InventoryManagement.Provider();
                Provider.ID = Cabang.ID_Provider;
                Provider.Select();

                InventoryManagement.NAS NAS = new InventoryManagement.NAS();
                NAS.ID = Cabang.ID_NAS;
                NAS.Select();

                InventoryManagement.Router Router = new InventoryManagement.Router();
                Router.ID = Cabang.ID_Router;
                Router.Select();

                InventoryManagement.Detail_Printer DetailPrinter = new InventoryManagement.Detail_Printer();
                DetailPrinter.ID_DataCabang = Cabang.ID;
                #endregion
                InventoryManagement.Printer Printer = new InventoryManagement.Printer();
                DataTable DT = DetailPrinter.SelectByParam();
                DT.Columns.Add("Title", typeof(string));
                for (int i = 0; i < DT.Rows.Count; i++)
                {
                    Printer.ID = Guid.Parse(DT.Rows[i]["ID_Printer"].ToString());
                    Printer.Select();
                    DT.Rows[i].BeginEdit();
                    DT.Rows[i]["Title"] = Printer.Title;
                    DT.Rows[i].AcceptChanges();
                }

                GridPrinter.DataSource = DT;
                GridPrinter.DataBind();
                #region cabang
                lblNamaCabang.Text = Cabang.NamaCabang;
                lblBandwidth.Text = Cabang.Bandwidth.ToString();
                lblDate.Text = Cabang.Date.Value.ToString("dd MMM yyyy");
                lblEngineer.Text = Cabang.Engineer;
                lblHP.Text = Cabang.HP;
                lblIPLAN.Text = Cabang.IPLAN;
                lblIPNAS.Text = Cabang.IPNAS;
                lblIPWAN.Text = Cabang.IPWAN;
                lblJmlNotebook.Text = Cabang.JumlahNotebook.ToString();
                lblJmlPC.Text = Cabang.JumlahPC.ToString();
                lblJmlTC.Text = Cabang.JumlahTC.ToString();
                lblKodeCabang.Text = Cabang.KodeCabang;
                lblNamaCabang.Text = Cabang.NamaCabang;
                lblNamaLokasi.Text = Cabang.NamaLokasi;
                lblNamaNAS.Text = Cabang.NamaNAS;
                lblPIC.Text = Cabang.PIC;
                lblSerialNumber.Text = Cabang.SerialNumber;
                lblSID.Text = Cabang.SID;
                lblTelp.Text = Cabang.Telp;
                txtALamat.Text = Cabang.Alamat;

                lblTipeNAS.Text = NAS.Title;
                lblTipeRouter.Text = Router.Title;
                lblProviderNAS.Text = Provider.Title;
                #endregion
            }
        }

        bool validasi()
        {
            bool check = true;
            return check;
        }


    }
}
